<div class="col-md-8">
<div class="main-content" id="main-content">
		<div class="sub-content page-header clearfix">
			<div class="top-header">
				<h1>Transfer Users</h1>
			</div><br/>
		<div class="middle-forms user-profile1 col-md-11">
		<form id="combinationForm" method="post" class="form-horizontal" role="form">
			<div class="sub-forms">
				<div class="form-group">
					<label class="control-label col-sm-3">From</label>
					<div class="select_6 col-sm-9">
						<select id="selectFrom" name="selectFrom" onchange="getcombinations()" class="form-control"> 
							<option value="">Select</option>
							<?foreach($this->allAdmins as $admins){?>
							<option value="<? echo $admins->user_id?>"><? echo $admins->company_name;?></option>
							<?}?>
						</select>
					</div> <span id="errorFrom" style="color:red"></span><br/>
				 </div>
				 <div class="form-group">
					<label class="control-label col-sm-3">To</label>
					<div class="select_6 col-sm-9">
						<select id="selectTo" name="selectTo" class="form-control"> 
							<option value="">Select</option>
							<?foreach($this->allAdmins as $admins){?>
							<option value="<? echo $admins->user_id?>"><? echo $admins->company_name;?></option>
							<?}?>
						</select>
					</div>&nbsp;&nbsp;&nbsp;<span>OR</span><span id="errorTo" style="color:red"></span><br/>
				 </div>&nbsp;&nbsp;&nbsp;<span><input type="checkbox" id="newAdmin" name="newAdmin" onclick="newAdminAdd()" >&nbsp;&nbsp;&nbsp;New Admin</span>
			</div>
			
			
			<div id="newAdminDiv" style="display:none">
			<p style="color:red">Please fill the below fields for admin registration</p><br/>
				<div class="form-group">
					<label class="control-label col-sm-3">Company Name</label>
					<div class="col-sm-9">
					<input  type="text" id="cname" name="cname" value="" class="form-control" placeholder="Company Name"></div>
				</div><span style="color:red" id="errorcname"></span>
				
				<div class="form-group">
					<label class="control-label col-sm-3">Email</label>
						<div class="col-sm-9"><input  type="text" id="email" name="email" value="" class="form-control" onblur="emailSucc()" placeholder="Email" /></div>
				</div><span id="error" style="color:red"></span> 
				  <span id="emptyemail" style="color:red"></span> 
				 <span style="color:red" id="erroremail"></span>
				
				<div class="form-group">
					<label class="control-label col-sm-3">Phone </label>
						<div class="col-sm-9"><input  type="text" id="phone" name="phone" value="" class="form-control" placeholder="Phone" /></div>	
				</div><span style="color:red" id="errorphone"></span>
			</div>
			
			<p style="color:red" id="sucMessage"></p><br /><br />
			<div id="combinationList" class="form-group">			
			</div>
			<div id="combinations_users" class="form-group">
			</div>
			<div class="botton_btns form-group">
			
					<a href="#" class="back_btn btn btn-danger" onclick="validate()" role="button">Transfer</a>
					<a class="back_btn btn btn-danger" href="<? echo $this->Url('users');?>/allusers-list" role="button">Back</a>
				
			</div>
			<input type="hidden" id="exit_email" name="exit_email" value="0">
			</form>
		</div>
	</div>
</div></div>
<script>
	function newAdminAdd(){
		if(document.getElementById("newAdmin").checked==true){
			$('#newAdminDiv').show();
		}else{
			$('#newAdminDiv').hide();
		}
	}
	function validate(){
		var flag=true;
		if($('#selectFrom').val()==''){
			$('#errorFrom').html('From Admin is required');
			var flag=false;	
		}else{
			$('#errorFrom').html('');
		}
		if(document.getElementById("newAdmin").checked==true){
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var letters = /^[a-zA-Z ]*$/;
		var validpattern = /^[+-]?\d+$/;
		var companyname=$('#cname').val();
		var emailcheck=$('#email').val();
		var pnumber=$('#phone').val();
		if(companyname==""){
			$('#errorcname').html('*Company name is required');	
			flag=false;
		}else{
			$('#errorcname').html('');
		}
		if(emailcheck==""){
		  $('#erroremail').html('*User email is required');
		  flag=false;
		}else if(!filter.test(emailcheck)){
		  $('#erroremail').html('Email format is not correct');
		  $("#error").html("");
		  flag=false;
		}else if(exit_email=='1'){
			$('#erroremail').html('');
			 $("#error").html("User is already exits");
			flag=false;
		}else{
		   $('#erroremail').html('');
		}
		if(pnumber==""){
		  $('#errorphone').html('*Phone number is required');
		  flag=false;
		}else if(!pnumber.match(validpattern)){
		     $('#errorphone').html('*Invalid phone number');
			   flag=false;
		}else{
		 $('#errorphone').html('');
		}
		}else{
			if($('#selectTo').val()==''){
				$('#errorTo').html('To Admin is required');
				var flag=false;	
			}else{
				$('#errorTo').html('');
			}
			if($('#selectTo').val()!='' && $('#selectFrom').val()!=''){
				if($('#selectTo').val()==$('#selectFrom').val()){
					alert('Please select unique admins');
					var flag=false;	
				}
			}
		}
		if(flag==false){
			return false;
		}else{
			var selectBoxes="";
			var checkboxes=document.getElementsByTagName("INPUT");	
			var len=checkboxes.length;	
			var cnt = 0;
			for(var i=0;i<len;i++)
			{		
				if(checkboxes[i].name=="checkCombination")
				{		
					if(checkboxes[i].checked)
					{
						selectBoxes+=checkboxes[i].value+'&&';
						cnt++;
						
					}
				}
			}
			if(cnt == 0) 
			{
				alert('Please select the combination(s) to transfer');
			}else{
				var fromAdminId=$('#selectFrom').val();
				if(document.getElementById("newAdmin").checked==true){
					$.ajax({
						type:	'POST',
						url:    BASE_URL+"/users/transfer-users",
						data:	{ids:selectBoxes,companyname:companyname,emailcheck:emailcheck,pnumber:pnumber,type:'new',fromAdminId:fromAdminId},
						success: function(data){
							$('#combinationList').html(data);
							$('#sucMessage').html('Transferd Succussesfully').delay(2000).fadeOut();
							window.location=BASE_URL+"/users/transfer-users";
						}
					});
				}else{
					var toAdminId=$('#selectTo').val();
					var fromAdminId=$('#selectFrom').val();
					$.ajax({
						type:	'POST',
						url:    BASE_URL+"/users/transfer-users",
						data:	{ids:selectBoxes,toAdminId:toAdminId,fromAdminId:fromAdminId,type:'old'},
						success: function(data){
							$('#combinationList').html(data);
							$('#sucMessage').html('Transferd Succussesfully').delay(2000).fadeOut();
						}
					});
				}
			}	
		}
	}
	function emailSucc(){
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		flag=true;
		  var emailcheck=$('#email').val();
		   if(emailcheck==""){
		     $('#erroremail').html('*User email is required');
			 flag=false;
		   }else if(!filter.test(emailcheck)){
			$('#erroremail').html('Email format is not correct');
			$("#error").html("");
			flag=false;
			}
			if(flag==false){ 
				return false;
			}else{
			   $.ajax({
				url:BASE_PATH+"/users/checkregemail",
				type:"POST",
				data:{email:emailcheck},
				success: function(data){
				 if(data.output=='success'){
					$("#exit_email").val('1');
					$('#erroremail').html('');
					$("#error").html("User is already exits");			 
					}else{
						$("#exit_email").val('0');
						$("#error").html("");
						$('#erroremail').html('');
					}
				},
			});
		}
   }
   function getcombinations(){
		$('#combinations_users').html('');
		var adminId=$('#selectFrom').val();
		$.ajax({
			url:BASE_URL+"/users/transfer-users",
			type:"POST",
			data:{adminId:adminId},
			success: function(data){
				$('#combinationList').html(data);
			},
		});
   }
   
   	function showUsers(combination_id,admin_user_id){
		$.ajax({
			url:BASE_URL+"/users/get-combination-users",
			type:"POST",
			data:{combination_id:combination_id,admin_user_id:admin_user_id},
			success: function(data){
				$('#combinations_users').html(data);
			},
		});
	}
	function hide_users(){
		$('#combinations_users').html('');
	}
</script>

        